import random
import hashlib
from django.conf import settings

def hash_code(s, salt=settings.SECRET_KEY):     # 加点盐
    h = hashlib.sha256()
    s += salt
    h.update(s.encode())    # update方法只接收bytes类型
    return h.hexdigest()

ALL_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
def gen_random_code(length=4):
    """生成指定长度的随机验证码"""
    return ''.join(random.choices(ALL_CHARS, k=length))
